<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Validator;

use Captcha;
use App\Models\User;
use App\Handlers\ImageUploadHandler;

use Overtrue\EasySms\EasySms;
use App\Http\Requests\Api\VerificationCodeRequest;

class UserController extends Controller
{

     //短信公共接口
    public function note($api,$data){

        //$data 请求地址信息 $api 请求的类型(比如登录、发送短信)

        //沙箱URL
        // $url = 'https://iosdatarecovery.test2.huduntech.com';
        //正式URL
        $url = 'http://api.huifudashi.com';

        //获取服务器时间
        $time = $url . "/v1/ios/time.php";
        //请求服务器，获取服务器时间
        $ch = curl_init($time);
        $options = array(
                            CURLOPT_RETURNTRANSFER => true,
                        );
        curl_setopt_array($ch,$options);
        $result = curl_exec($ch);
        curl_close($ch);

        //获取服务器时间
        $time = json_decode($result,true)['responseData']['time'];
        //拼接服务器时间并md5加密 在拼接时间,获的sign //短信签名(sign)
        $sign = md5('ZGZhc2ZqYW9zZm5tbGFmajE1NDU=' . $time) . $time;
        //s_id:产品id 周点点第一款产品id为38 苏浩第一款产品id为42 类型：int
        $s_id = 42;
        //softname:产品名称 类型：string
        $softname = '先锋恢复精灵';
        

        $data = $url . $api . '?s_id=' .  $s_id . '&softname=' . $softname .  $data . '&sign=' . $sign;

        //初始化,请求信息并返回数据
        $url = curl_init($data);
        $options = array(
                            CURLOPT_RETURNTRANSFER => true,
                        );
        curl_setopt_array($url,$options);
        $result = curl_exec($url);
        curl_close($url);

        //返回数据
        return $result;

    } 

    //短信发送接口
    public function dx($mobile){

        //请求的接口地址
        $api = '/v1/ios/sms.php';
        //tem_id:短信模板id 登陆获取验证码模板id为5 类型：int
        $tem_id = 5;
        //请求的手机参数
        $data = '&tem_id=' . $tem_id . '&mobile=' . $mobile;

        //调用公共短信方法
        $result = $this->note($api,$data);

        //转为数组
        $statu = json_decode($result,true)['code'];

        //判断是否发送成功
        if($statu == 1){

          return ['code'=>$statu]; 
           
        }else{

            return ['code'=>0];
        }
        

    }


    //短信登录接口
    public function login($mobile,$note){

        //mobile 获取手机号 $note 获取验证码
        //调用接口
        $api = '/v1/ios/login.php';

        //请求参数
        $data = '&device_id=123' . '&code=' . $note . '&mobile=' . $mobile;

        //调用公共短信方法
        $result = $this->note($api,$data);

        //转为数组并获取状态
        $data = json_decode($result,true)['code'];

        //判断用户是否登录成功    
        if($data == 30001){

            return ['status'=>0,'code'=>'验证码错误'];

        }elseif($data == 30000){

            //判断当前用户是否存在数据库
            $user = User::where('phone',$mobile)->first();

            if(is_null($user)){

                //随机字符
                $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
                //读取随机字符
                $pwd = substr(str_shuffle($chars),mt_rand(0,strlen($chars)-11),10);
                //用户默认信息
                $data = ['avatar'=>'/avatar/avatar.jpg','phone'=>$mobile,'name'=>'小清新用户','vip'=>'0','password'=>md5($pwd),'created_at'=>date('Y-m-d H:i:s')];
                //录入用户信息
                $succees =User::insertGetId($data);
                //判断是否添加成功
                if($succees){

                    //获取当前添加用户
                    $user = User::find($succees);

                    //返回用户详细信息
                    return ['status'=>1,'user'=>$user];

                }else{

                    return ['status'=>0];
                }

            }else{

                //返回用户详细信息
                return ['status'=>1,'user'=>$user];
               
            }
            

        }else{

            $status = '系统出错';

        }

    }

    //用户信息修改
    public function edit(Request $request,ImageUploadHandler $upload){

            //获取type修改方式
            if($request->type == 'avatar'){

                //头像路径
                $folder = 'avatar';
                //上传地址
                $file = $upload->save($request->avatar,$folder);
                //修改当前用户头像
                $status = User::where('id',$request->uid)->update(['avatar'=>$file['path']]);
                //获取用户头像
                $path = User::where('id',$request->uid)->select('avatar')->first();

                return ['status'=>1,'code'=>'修改头像成功','path'=>$path];

            }elseif($request->type == 'name'){

                //修改当前用户名称
                $status = User::where('id',$request->uid)->update(['name'=>$request->name]);

                return ['status'=>1,'code'=>'修改名称成功'];

            }else{

                return ['status'=>0,'code'=>'修改失败'];
            }

    }


}
